<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.nickname"
        placeholder="请输入专家昵称"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />

      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" icon="el-icon-refresh" @click="resetFilter">
        重置
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
        新增专家
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="专家ID" prop="expert_id" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.expert_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="头像" width="80" align="center">
        <template slot-scope="{row}">
          <el-avatar :size="40" :src="row.avatar" icon="el-icon-user-solid" />
        </template>
      </el-table-column>
      <el-table-column label="昵称" min-width="120px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleUpdate(row)">{{ row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="竞彩单关" width="120px" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.jing_dan" type="primary" size="small">有</el-tag>
          <el-tag v-else type="info" size="small">无</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="竞彩二串1" width="120px" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.jing_shuang" type="success" size="small">有</el-tag>
          <el-tag v-else type="info" size="small">无</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="竞彩三串1" width="120px" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.jing_san" type="warning" size="small">有</el-tag>
          <el-tag v-else type="info" size="small">无</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="让球单关" width="120px" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.rang_dan" type="primary" size="small">有</el-tag>
          <el-tag v-else type="info" size="small">无</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="让球二串1" width="120px" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.rang_shuang" type="success" size="small">有</el-tag>
          <el-tag v-else type="info" size="small">无</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="让球三串1" width="120px" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.rang_san" type="warning" size="small">有</el-tag>
          <el-tag v-else type="info" size="small">无</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="让分单关" width="120px" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.fen_dan" type="primary" size="small">有</el-tag>
          <el-tag v-else type="info" size="small">无</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="让分二串1" width="120px" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.fen_shuang" type="success" size="small">有</el-tag>
          <el-tag v-else type="info" size="small">无</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="让分三串1" width="120px" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.fen_san" type="warning" size="small">有</el-tag>
          <el-tag v-else type="info" size="small">无</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="文章标题" min-width="150px">
        <template slot-scope="{row}">
          <span v-if="row.title" style="font-weight: bold;">{{ row.title }}</span>
          <span v-else style="color: #999;">暂无标题</span>
        </template>
      </el-table-column>
      <el-table-column label="大小球标题" min-width="150px">
        <template slot-scope="{row}">
          <span v-if="row.title1" style="font-weight: bold;">{{ row.title1 }}</span>
          <span v-else style="color: #999;">暂无标题</span>
        </template>
      </el-table-column>
      <el-table-column label="让球标题" min-width="150px">
        <template slot-scope="{row}">
          <span v-if="row.title2" style="font-weight: bold;">{{ row.title2 }}</span>
          <span v-else style="color: #999;">暂无标题</span>
        </template>
      </el-table-column>
      <el-table-column label="文章内容" min-width="200px">
        <template slot-scope="{row}">
          <div v-if="row.free_content" style="max-height: 100px; overflow: hidden; text-overflow: ellipsis;" v-html="row.free_content" />
          <span v-else style="color: #999;">暂无内容</span>
        </template>
      </el-table-column>
      <el-table-column min-width="120px" align="center">
        <template slot="header">
          <div>
            <span>推荐次数</span>
            <el-tooltip content="这个值越低优先选择这个专家" placement="top">
              <i class="el-icon-question" style="margin-left: 5px; color: #409EFF; cursor: help;" />
            </el-tooltip>
          </div>
        </template>
        <template slot-scope="{row}">
          <span>{{ row.recommend_count || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :page_size.sync="listQuery.page_size" @pagination="getList" />

    <!-- 专家编辑对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="800px">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="专家ID">
              <el-input v-model="temp.expert_id" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="昵称">
              <el-input v-model="temp.nickname" readonly />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="头像">

          <div v-if="temp.avatar" style="margin-top: 10px;">
            <el-avatar :size="60" :src="temp.avatar" icon="el-icon-user-solid" />
          </div>
        </el-form-item>

        <el-divider content-position="left">竞彩数据</el-divider>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="竞彩单关" prop="jing_dan">
              <el-input
                v-model="temp.jing_dan"
                type="textarea"
                :rows="4"
                placeholder="竞彩单关"
                maxlength="2000"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="竞彩二串1" prop="jing_shuang">
              <el-input
                v-model="temp.jing_shuang"
                type="textarea"
                :rows="4"
                placeholder="竞彩二串1"
                maxlength="2000"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="竞彩三串1" prop="jing_san">
              <el-input
                v-model="temp.jing_san"
                type="textarea"
                :rows="4"
                placeholder="竞彩三串1"
                maxlength="2000"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">让球数据(非竞彩用)</el-divider>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="让球单关" prop="rang_dan">
              <el-input
                v-model="temp.rang_dan"
                type="textarea"
                :rows="4"
                placeholder="足球单关"
                maxlength="2000"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="让球二串1" prop="rang_shuang">
              <el-input
                v-model="temp.rang_shuang"
                type="textarea"
                :rows="4"
                placeholder="足球二串1"
                maxlength="2000"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="让球三串1" prop="rang_san">
              <el-input
                v-model="temp.rang_san"
                type="textarea"
                :rows="4"
                placeholder="足球三串1"
                maxlength="2000"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">让分数据</el-divider>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="让分单关" prop="fen_dan">
              <el-input
                v-model="temp.fen_dan"
                type="textarea"
                :rows="4"
                placeholder="让分单关"
                maxlength="2000"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="让分二串1" prop="fen_shuang">
              <el-input
                v-model="temp.fen_shuang"
                type="textarea"
                :rows="4"
                placeholder="让分二串1"
                maxlength="2000"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="让分三串1" prop="fen_san">
              <el-input
                v-model="temp.fen_san"
                type="textarea"
                :rows="4"
                placeholder="让分三串1"
                maxlength="2000"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">文章内容</el-divider>

        <el-form-item label="文章标题" prop="title">
          <el-input
            v-model="temp.title"
            placeholder="请输入文章标题"
            maxlength="100"
            show-word-limit
          />
        </el-form-item>

        <el-form-item label="大小球标题" prop="title1">
          <el-input
            v-model="temp.title1"
            placeholder="请输入大小球标题"
            maxlength="100"
            show-word-limit
          />
        </el-form-item>

        <el-form-item label="让球标题" prop="title2">
          <el-input
            v-model="temp.title2"
            placeholder="请输入让球标题"
            maxlength="100"
            show-word-limit
          />
        </el-form-item>

        <el-form-item label="文章内容">
          <WangEditor :content="freeContentData" @submit="workVadeUpdateFreeContent" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?updateData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <!-- 专家选择器 -->
    <ExpertSelector
      :visible.sync="showExpertSelector"
      @select="handleExpertSelect"
    />
  </div>
</template>

<script>
import { zhuanjiaList as fetchExpertList, zhuanjiaInfo, zhuanjiaSave, deleteExpert } from '../../../libs/api'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import ExpertSelector from '@/components/ExpertSelector.vue'
import WangEditor from '@/components/richText'

export default {
  name: 'ExpertList',
  components: { Pagination, ExpertSelector, WangEditor },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 20,
        nickname: undefined,
        expert_id: undefined,
        title: true
      },
      temp: {
        id: undefined,
        expert_id: '',
        nickname: '',
        avatar: '',
        jing_dan: '',
        jing_shuang: '',
        jing_san: '',
        da_dan: '',
        da_shuang: '',
        da_san: '',
        bi_dan: '',
        bi_shuang: '',
        bi_san: '',
        rang_dan: '',
        rang_shuang: '',
        rang_san: '',
        fen_dan: '',
        fen_shuang: '',
        fen_san: '',
        title: '',
        title1: '',
        title2: '',
        free_content: '',
        recommend_count: 0
      },
      freeContentData: '',
      dialogFormVisible: false,
      dialogStatus: '',
      showExpertSelector: false,
      textMap: {
        update: '编辑专家数据',
        create: '新增专家'
      },
      rules: {
        // 只对可编辑的字段进行验证
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getExpertInfo(item) {
      const res = await zhuanjiaInfo({
        expert_id: item.expert_id
      })
      this.temp = res.data
      this.freeContentData = res.data.free_content || ''
    },
    getList() {
      this.listLoading = true
      fetchExpertList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20,
        nickname: undefined,
        expert_id: undefined,
        title: true
      }
      this.getList()
    },
    // 新增专家
    handleCreate() {
      this.showExpertSelector = true
    },
    // 专家选择回调
    handleExpertSelect(expert) {
      this.resetTemp()
      this.temp.expert_id = expert.id
      this.temp.nickname = expert.nickname
      this.temp.avatar = expert.avatar
      this.freeContentData = ''
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 编辑专家
    async handleUpdate(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true

      // 先获取真实的专家数据
      await this.getExpertInfo(row)

      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row) {
      this.$confirm('确定要删除这个专家吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteExpert({
          id: row.id
        }).then(() => {
          this.getList()
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    // 根据id判断是新增还是更新
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          zhuanjiaSave(tempData).then(() => {
            this.getList()
            this.dialogFormVisible = false
            const message = tempData.id ? '更新成功' : '新增成功'
            this.$notify({
              title: '成功',
              message: message,
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        expert_id: '',
        nickname: '',
        avatar: '',
        jing_dan: '',
        jing_shuang: '',
        jing_san: '',
        da_dan: '',
        da_shuang: '',
        da_san: '',
        bi_dan: '',
        bi_shuang: '',
        bi_san: '',
        rang_dan: '',
        rang_shuang: '',
        rang_san: '',
        fen_dan: '',
        fen_shuang: '',
        fen_san: '',
        title: '',
        title1: '',
        title2: '',
        free_content: '',
        recommend_count: 0
      }
      this.freeContentData = ''
    },
    // 富文本编辑器回调
    workVadeUpdateFreeContent(content) {
      this.temp.free_content = content
    }
  }
}
</script>

<style scoped>
.link-type {
  color: #409EFF;
  cursor: pointer;
}
.link-type:hover {
  color: #66b1ff;
}
</style>
